<?php
/**
 * File management
 *
 * @author      Hyper <hyper@flashbay.com>
 * @copyright   Copyright (c) 2011 (http://www.saleshero.com)
 */
class FileController extends SalesHero_Controller_Action
{
    public $folderModel = null;

    public $fileModel = null;

    public function init()
    {
        $this->folderModel = new Application_Model_Folder();
        $this->fileModel = new Application_Model_File();
    }

    public function indexAction()
    {
    }

    public function folderAction()
    {
        $this->view->pageTitle = 'Folder Management';

        $url = $this->view->url = $this->view->url();

        $request = $this->getRequest();
        $id = $this->view->id = $request->getParam('id', 0);

        if ($request->isPost()) {
            $params = $request->getParams();

            if ($params['op'] == 'add') {
                try {
                    $this->view->id = $id = $this->folderModel->add($params);
                } catch (Exception $e) {
                    $this->feedback(
                        array(
                            'title'     => 'Add new folder failed',
                            'message'   => $e->getMessage(),
                        )
                    );
                }
            } elseif ($params['op'] == 'update') {
                try {
                    $this->folderModel->update($params);
                } catch (Exception $e) {
                    $this->feedback(
                        array(
                            'title'     => 'Update folder info failed',
                            'message'   => $e->getMessage(),
                        )
                    );
                }
            } elseif ($params['op'] == 'delete') {
                try {
                    $this->folderModel->delete($id);
                    $this->getHelper('Redirector')->gotoUrl($url);
                } catch (Exception $e) {
                    $this->feedback(
                        array(
                            'title'     => 'Delete folder failed',
                            'message'   => $e->getMessage(),
                        )
                    );
                }
            }
        }

        if ($id > 0) {
            $this->view->info = $info = $this->folderModel->getInfo($id);
        }

        $this->view->folders = $this->folderModel->getFolders();
        $this->view->menus = $this->folderModel->menuOption((int) @$info['parent_id']);
    }

    public function listAction()
    {
        $this->view->pageTitle = 'File List';

        $request = $this->getRequest();
        $folderId = $this->view->folderId = $request->getParam('folder_id', 0);
        $keyword = trim($request->getParam('keyword'));
        $page = $request->getParam('page', 1);

        $select = $this->db->select()->from('file')
                    ->where('folder_id = ?', $folderId)
                    ->order('created DESC');

        if (!empty($keyword)) {
            $select->where('filename like ?', "%$keyword%");
        }

        $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(30)->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->params = $request->getParams();
        $this->view->folders = $this->folderModel->getFolders();
        $this->view->keyword = $keyword;
    }

    public function deleteAction()
    {
        $request = $this->getRequest();
        $id = $request->getParam('id');

        try {
            $url = isset($_SERVER['HTTP_REFERER']) ?
                        $_SERVER['HTTP_REFERER'] : '/file/list';

            $this->fileModel->delete($id);
            $this->getHelper('Redirector')->gotoUrl($url);
        } catch (Exception $e) {
            $this->feedback(
                array(
                    'title'     => 'Delete file failed',
                    'message'   => $e->getMessage(),
                )
            );
        }
    }

    public function editAction()
    {
        $this->view->pageTitle = 'Edit File Infomation';

        $request = $this->getRequest();
        $id = $request->getParam('id');

        if ($request->isPost()) {
            $params = $request->getParams();
            try {
                $this->fileModel->update($params);
                $this->feedback(array(
                    'title'     => 'Update file info successfully',
                    'message'   => 'Update file info successfully',
                    'redirect'  => '/file/list/folder_id/' . $params['folder_id'],
                    'linktext'  => 'File List'
                ));
            } catch (Exception $e) {
                $this->feedback(
                    array(
                        'title'     => 'Modify file info failed',
                        'message'   => $e->getMessage(),
                    )
                );
            }
        }

        $info = $this->view->info = $this->fileModel->getInfo($id);
        $this->view->menus = $this->folderModel->menuOption((int) @$info['folder_id']);
    }

    public function transferAction()
    {
        $this->view->pageTitle = 'Transfer Files';

        $request = $this->getRequest();
        $from = $request->getParam('from', 0);
        $to = $request->getParam('to', 0);

        if ($request->isPost()) {
            try {
                $this->folderModel->transfer($from, $to);
                $this->feedback(array(
                    'title'     => 'Transfer files successfully',
                    'message'   => 'Transfer files successfully',
                    'redirect'  => '/file/list/folder_id/' . $to,
                    'linktext'  => 'Visit destination folder'
                ));
            } catch (Exception $e) {
                $this->feedback(
                    array(
                        'title'     => 'Transfer files failed',
                        'message'   => $e->getMessage(),
                    )
                );
            }
        }

        $this->view->menus1 = $this->folderModel->menuOption((int) $from);
        $this->view->menus2 = $this->folderModel->menuOption((int) $to);
    }

    public function uploadAction()
    {
        $this->view->pageTitle = 'Upload File';
        $request = $this->getRequest();
        $folderId = $request->getParam('folder_id', 0);

        if ($request->isPost()) {
            try {
                $result = $this->fileModel->upload($folderId, $_FILES['file']);
                $result['description'] = $request->getParam('description');

                $this->fileModel->add($result);

                $this->getHelper('Redirector')->gotoUrl("/file/list/folder_id/$folderId");
            } catch (Exception $e) {
                $this->feedback(
                    array(
                        'title'     => 'Upload file failed',
                        'message'   => $e->getMessage(),
                    )
                );
            }
        }

        $this->view->menus = $this->folderModel->menuOption($folderId);
    }
}

